import 'dart:async';

import 'package:flutter/services.dart';

typedef MessageCall = void Function(dynamic msgJson);
typedef AuthCallBack<T> = void Function(T? value);
typedef PayCallBack<T> = void Function(T? value);
class AliPlugin {
  static const MethodChannel _channel =
      const MethodChannel('ali_plugin');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
  static Future<String?> get deviceId async {
    if(_deviceId != null){
      return _deviceId;
    }
    final String? deviceId = await _channel.invokeMethod('getDeviceId');
    return deviceId;
  }
  static String? _deviceId;
  void setDeviceId(String id) {
    _deviceId = id;
  }
  static void bindAccount(String account) async {
    _channel.invokeMethod('bindAccount', account);
    _channel.setMethodCallHandler(_onMethodCall);
  }

  static void unBindAccount() async {
    _channel.invokeMethod('unBindAccount');
  }

  static AliPushMessageListener? _messageListener;

  static set messageListener(AliPushMessageListener value) {
    _messageListener = value;
  }

  static AliBindPushListener? _bindListener;

  static set bindListener(AliBindPushListener value) {
    _bindListener = value;
  }

  static Future<dynamic> _onMethodCall(MethodCall call) async{
    dynamic arguments = call.arguments;
    print('AliPlugin._onMethodCall - ${call.method}-$arguments');
    switch(call.method) {
      case 'onBindSuccess':
        if(_bindListener != null){
          _bindListener!.onBindSuccess(arguments);
        }
        break;
      case 'onBindFailed':
        if(_bindListener != null){
          _bindListener!.bindFailed(arguments);
        }
        break;
      case 'unBindSuccess':
        if(_bindListener != null){
          _bindListener!.onUnBindSuccess(arguments);
        }
        break;
      case 'onNotification':
        if(_messageListener != null){
          _messageListener!.onNotification(arguments);
        }
        break;
      case 'onMessage':
        if(_messageListener != null){
          _messageListener!.onMessage(arguments);
        }
        break;
      case 'onNotificationOpened':
        if(_messageListener != null){
          _messageListener!.onNotificationOpened(arguments);
        }
        break;
      case 'authCallBack':
        if(_authCallBack != null){
          _authCallBack!.call(call.arguments);
        }
        break;
      case 'payCallBack':
        if(_payCallBack != null){
          _payCallBack!.call(call.arguments);
        }
        break;
      default:
        print('ali_push-platformMethod:${call.method}--argument:$arguments');
        return null;
    }
    return null;
  }

  static AuthCallBack? _authCallBack;

  static set authCallBack(AuthCallBack value) {
    _authCallBack = value;
  }

  static PayCallBack? _payCallBack;

  static set payCallBack(PayCallBack value){
    _payCallBack = value;
  }

  static void toAuth(String authInfo) {
    _channel.setMethodCallHandler(_onMethodCall);
    _channel.invokeMethod('toAuth', authInfo);
  }

  static void payOrder(String orderStr) {
    _channel.setMethodCallHandler(_onMethodCall);
    _channel.invokeMethod('payOrder', orderStr);
  }
}
class AliBindPushListener {
  AliBindPushListener({
    this.onBindSuccessCall, this.onBindFailedCall, this.onUnBindSuccessCall, });

  MessageCall? onBindSuccessCall;
  MessageCall? onBindFailedCall;
  MessageCall? onUnBindSuccessCall;
  void onBindSuccess(String? value){
    if(onBindSuccessCall != null){
      onBindSuccessCall!.call(value);
    }
  }
  void bindFailed(dynamic value){
    if(onBindFailedCall != null){
      onBindFailedCall!.call(value);
    }
  }
  void onUnBindSuccess(String? value){
    if(onUnBindSuccessCall != null){
      onUnBindSuccessCall!.call(value);
    }
  }

}
class AliPushMessageListener {
  AliPushMessageListener({
    this.onNoticationCall, this.onMessageCall, this.onNotificationOpenedCall});
  MessageCall? onNoticationCall;
  MessageCall? onMessageCall;
  MessageCall? onNotificationOpenedCall;
  void onNotification(dynamic arguments){
    if(onNoticationCall != null){
      onNoticationCall!.call(arguments);
    }
  }
  void onMessage(dynamic arguments){
    if(onMessageCall != null){
      onMessageCall!.call(arguments);
    }
  }
  void onNotificationOpened(dynamic arguments){
    if(onNotificationOpenedCall != null){
      onNotificationOpenedCall!.call(arguments);
    }
  }

}
